﻿using System;
using System.Data.Common;
using System.Linq;

namespace Centido.Management.ConsoleManager
{
    /// <summary>
    ///   A console command that adds the specified user to the specified business unit.
    /// </summary>
    internal class AddUserInUnitCommand : Command
    {
        /// <summary>
        ///   The unit name parameter.
        /// </summary>
        public const string UnitNameParameter = "unit";

        /// <summary>
        ///   The user name parameter.
        /// </summary>
        public const string UserNameParameter = "user";


        /// <summary>
        ///   Gets the console command name.
        /// </summary>
        public override string Name
        {
            get { return "adduserinunit"; }
        }


        /// <summary>
        ///   Gets the console command description.
        /// </summary>
        public override string Description
        {
            get { return Messages.AddUserInUnitCommandDescription; }
        }


        /// <summary>
        ///   Gets the console command full description.
        /// </summary>
        public override string FullDescription
        {
            get { return Messages.AddUserInUnitCommandFullDescription; }
        }


        /// <summary>
        ///   Executes the console command.
        /// </summary>
        /// <param name="context">The console command execution context.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="context"/> is null.</exception>
        public override void Execute(CommandExecutionContext context)
        {
            base.Execute(context);

            // If the user name or unit name was not specified
            if (!context.ParameterValueIsNotEmpty(UserNameParameter)
                || !context.ParameterValueIsNotEmpty(UnitNameParameter))
            {
                Console.WriteLine(Messages.AddUserInUnitCommandNotAllParametersSpecifiedError);
                return;
            }

            var domain = context.CurrentDomain;

            if (domain == null) return;

            // The user name
            var userName = context.GetParameterValue(UserNameParameter);

            // The unit name
            var unitName = context.GetParameterValue(UnitNameParameter);

            try
            {
                // Trying to find a user with the specified name
                var user = domain.GetUser(userName);

                // If there is no user with the specified name
                if (user == null)
                {
                    Console.WriteLine(Messages.CannotFindUserError, userName);
                    return;
                }

                // Trying to find a unit with the specified name
                var unit = domain.GetUnit(unitName);

                // If there is no unit with the specified name
                if (unit == null)
                {
                    Console.WriteLine(Messages.CannotFindUnitError, unitName);
                    return;
                }

                if (!unit.Users.Any(u => u.Name == user.Name))
                    unit.AddUser(user);
            }
            catch (DbException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                return;
            }

            // Displaying the success message
            Console.WriteLine(Messages.CommandCompletedMessage);
        }
    }
}
